<?php

/**
 * crpCalendar
 *
 * @copyright (c) 2007,2009 Daniele Conca
 * @link http://code.zikula.org/crpcalendar Support and documentation
 * @author Daniele Conca <conca.daniele@gmail.com>
 * @license GNU/GPL - v.2.1
 * @package crpCalendar
 */

Loader :: includeOnce('modules/crpCalendar/pnclass/crpCalendar.php');

/**
 * the main user function
 *
 * @return string HTML string
 */
function crpCalendar_user_main()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	// Create output object
	$pnRender = new pnRender('crpCalendar', false);

	// Return the output that has been generated by this function
	return $pnRender->fetch('crpcalendar_user_main.htm');
}

/**
 * view items list
 *
 * @return string HTML output
 */
function crpCalendar_user_view()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listEvents();
}

/**
 * view year items
 *
 * @return string HTML output
 */
function crpCalendar_user_year_view()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listYearEvents();
}

/**
 * view month items
 *
 * @return string HTML output
 */
function crpCalendar_user_month_view()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listMonthEvents();
}

/**
 * view week items
 *
 * @return string HTML output
 */
function crpCalendar_user_week_view()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listWeekEvents();
}

/**
 * view day items
 *
 * @return string HTML output
 */
function crpCalendar_user_day_view()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listDayEvents();
}

/**
 * display item
 *
 * @return string HTML output
 */
function crpCalendar_user_display()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->displayEvent();
}

/**
 * diaplay item, for content module
 *
 * @return string HTML output
 */
function crpCalendar_user_simple_display($args)
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	if (!pnModAvailable('crpCalendar'))
		return;

	$calendar = new crpCalendar();
	return $calendar->simpleDisplayEvent($args['eventid']);
}

/**
 * diaplay category items, for content module
 *
 * @return string HTML output
 */
function crpCalendar_user_simple_category_display($args)
{
	// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	if (!pnModAvailable('crpCalendar'))
		return;

	$modvars = pnModGetVar('crpCalendar');

	$apiargs['interval'] = $args['limit'];
	$apiargs['startnum'] = 1;
	$apiargs['category'] = $args['categoryid'];
	$apiargs['active'] = 'A';
	$apiargs['modvars'] = $modvars;

	// call the api
	$items = pnModAPIFunc('crpCalendar', 'user', 'getall', $apiargs);

	$calendar = new crpCalendar();
	$htmlText = "";

	foreach ($items as $kEvent => $vEvent)
	{
		$htmlText .= $calendar->simpleDisplayEvent($vEvent['eventid']);
	}
	return $htmlText;
}

/**
 * get event's image
 *
 * @return blob image
 */
function crpCalendar_user_get_image()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->dao->getImage();
}

/**
 * get user's partecipations
 *
 * @return html
 */
function crpCalendar_user_get_partecipations()
{
// Security check
	if (!pnModGetVar('crpCalendar', 'enable_partecipation'))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listUserPartecipations();
}

/**
 * get attendees partecipations
 *
 * @return html
 */
function crpCalendar_user_get_attendees()
{
// Security check
	if (!pnModGetVar('crpCalendar', 'enable_partecipation'))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listAttendees();
}

/**
 * get event's file
 *
 * @return blob image
 */
function crpCalendar_user_get_file()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$eventid = pnVarCleanFromInput('eventid');

	$calendar = new crpCalendar();
	$document = $calendar->dao->getFile($eventid, 'document', true);

	Header("Content-type: {$document['content_type']}");
	Header("Content-Disposition: inline; filename={$document['name']}");
	//Header("Content-Length: " . strlen($file['binary_data']));

	echo $document['binary_data'];

	pnShutDown();
}

/**
 * get event's thumbnail thru gd library
 *
 * @return blob image
 */
function crpCalendar_user_get_thumbnail()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->getThumbnail();
}

/**
 * new item
 *
 * @return string HTML output
 */
function crpCalendar_user_new()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_COMMENT))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->submitEvent();
}

/**
 * create item
 *
 * @return string HTML output
 */
function crpCalendar_user_create()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_COMMENT))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->createEvent();
}

/**
 * modify item
 *
 * @return string HTML output
 */
function crpCalendar_user_modify()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_MODERATE))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->editEvent();
}

/**
 * update item
 *
 * @return string HTML output
 */
function crpCalendar_user_update()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_MODERATE))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->updateEvent();
}

/**
 * feed items
 *
 * @return string HTML output
 */
function crpCalendar_user_getfeed()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->getFeed();
}

/**
 * build ical output
 *
 * @return string HTML output
 */
function crpCalendar_user_getICal()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->getICal();
}

/**
 * build ical output
 *
 * @return string HTML output
 */
function crpCalendar_user_ical_exports()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->listICal();
}

/**
 * add user's partecipation
 *
 * @return string HTML output
 */
function crpCalendar_user_add_partecipation()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ) || !pnUserLoggedIn() || !pnModGetVar('crpCalendar', 'enable_partecipation'))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->addPartecipation();
}

/**
 * delete user's partecipation
 *
 * @return string HTML output
 */
function crpCalendar_user_delete_partecipation()
{
// Security check
	if (!SecurityUtil :: checkPermission('crpCalendar::', '::', ACCESS_READ) || !pnUserLoggedIn() || !pnModGetVar('crpCalendar', 'enable_partecipation'))
	{
		return LogUtil :: registerPermissionError();
	}

	$calendar = new crpCalendar();
	return $calendar->deletePartecipation();
}